package com.kc.sjwh.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Zzjgsy;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.service.ZzjgsyService;

/**
 * 组织结构使用
 * @author zhouy
 *
 */
@Controller
@RequestMapping("zzjgsy")
public class ZzjgsyAction extends BaseAction{
	@Autowired
	ZzjgsyService zzjgsyService;
	
	@RequestMapping("page")
	public String page(HttpServletRequest req) {
		return "zzjgsy/zzjgsyForm";
	}
	
	/**
	 * 获得可选应用
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getYy")
	public String getYy(HttpServletRequest req){
		AjaxRes ajaxRes = new AjaxRes();
		List<Zzjgsy> list = zzjgsyService.getYy();
		String code = "<option value='-1' selected='selected'>--请选择应用--</option>";
		for(int i = 0 ; i < list.size() ; i++){
			code += "<option value='" + list.get(i).getZdbh() + "'>" + list.get(i).getZdxm() + "</option>";
		}
		ajaxRes.setData(code);
		return JSON.toJSONString(ajaxRes);
	}
	
	/**
	 * 加载所有资源
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("loadAllZzRes")
	public String loadAllZzRes(HttpServletRequest req){
		AjaxRes ajaxRes = new AjaxRes();
		QueryParam p = new QueryParam();
		String item = req.getParameter("item");
		if(null != item){
			item = item.trim();
			if(!"".equals(item)){
				p.addSqlCondition(" o.dwmc like '%" + item + "%'");
			}
		}
		List<Zzjgsy> list = zzjgsyService.getAllZzRes(p);
		//获取已有资源
		String zdbh = req.getParameter("zdbh");
		p = new QueryParam();
		p.addSqlCondition(" t.zdbh = '" + zdbh + "'");
		List<Zzjgsy> have = zzjgsyService.getHaveRes(p);
		String ids = "";
		for(Zzjgsy one : have){
			ids += one.getDwdm() + ",";
		}
		if(ids.length() > 0){
			ids = ids.substring(0, ids.length() - 1);
		}
		Map map = new HashMap();
		map.put("allData", list);
		map.put("ids", ids);
		ajaxRes.setData(map);
		return JSON.toJSONString(ajaxRes);
	}
	
	/**
	 * 保存编辑
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(HttpServletRequest req){
		String ids = req.getParameter("zyids");
		if(null != ids && ids.length() > 0){
			String[] idArr = ids.split(",");
			//先删除原数据
			String zdbh = req.getParameter("zdbh");
			zzjgsyService.deleteOldData(zdbh);
			//添加新数据
			for(int i = 0 ; i < idArr.length ; i++){
				Zzjgsy one = new Zzjgsy();
				String id = UUID.randomUUID().toString().replace("-", "");
				one.setZzjgsyid(id);
				one.setZdbh(zdbh);
				one.setDwdm(idArr[i]);
				one.setCjr("");
				zzjgsyService.addNewData(one);
			}
		}
		return JSON.toJSONString(new AjaxRes());
	}
}
